/**
 * 环形脉冲可视化器
 */
class CircularPulseVisualizer extends VisualizerInterface {
    constructor(canvas, ctx) {
        super(canvas, ctx);
        this.rings = [];
        this.pulses = [];
    }

    getInfo() {
        return {
            name: 'Circular Pulse',
            description: '环形脉冲频谱可视化',
            author: 'AudioNN',
            version: '1.0.0',
            category: 'abstract'
        };
    }

    getDefaultSettings() {
        return {
            ...super.getDefaultSettings(),
            ringCount: 8,
            pulseCount: 20,
            maxRadius: 300,
            pulseSpeed: 3,
            rotationSpeed: 0.01
        };
    }

    init(config = {}) {
        super.init(config);
        this.initializeRings();
        this.currentRotation = 0;
        return this;
    }

    initializeRings() {
        this.rings = [];
        for (let i = 0; i < this.settings.ringCount; i++) {
            this.rings.push({
                radius: (i + 1) * (this.settings.maxRadius / this.settings.ringCount),
                intensity: 0,
                rotation: 0,
                segments: 32 + i * 8
            });
        }
    }

    render(frequencyData, cachedData = null, currentTime = 0) {
        this.clearCanvas();
        
        const dataToUse = cachedData || frequencyData;
        if (!dataToUse) return;

        const width = this.canvas.width / window.devicePixelRatio;
        const height = this.canvas.height / window.devicePixelRatio;
        const centerX = width / 2;
        const centerY = height / 2;

        this.currentRotation += this.settings.rotationSpeed;

        // 更新环形数据
        this.updateRings(dataToUse);

        // 绘制脉冲环
        this.drawRings(centerX, centerY);

        // 绘制中心脉冲
        this.drawCenterPulse(dataToUse, centerX, centerY);
    }

    updateRings(frequencyData) {
        const dataPerRing = frequencyData.length / this.rings.length;
        
        this.rings.forEach((ring, index) => {
            const startIndex = Math.floor(index * dataPerRing);
            const endIndex = Math.floor((index + 1) * dataPerRing);
            
            let avgIntensity = 0;
            for (let i = startIndex; i < endIndex; i++) {
                avgIntensity += frequencyData[i];
            }
            avgIntensity /= (endIndex - startIndex);
            
            ring.intensity = avgIntensity / 255;
            ring.rotation = this.currentRotation * (index + 1) * 0.5;
        });
    }

    drawRings(centerX, centerY) {
        this.rings.forEach((ring, ringIndex) => {
            const segmentAngle = (Math.PI * 2) / ring.segments;
            
            for (let i = 0; i < ring.segments; i++) {
                const angle = i * segmentAngle + ring.rotation;
                const intensity = ring.intensity;
                
                if (intensity > 0.1) {
                    const x1 = centerX + Math.cos(angle) * ring.radius;
                    const y1 = centerY + Math.sin(angle) * ring.radius;
                    const x2 = centerX + Math.cos(angle) * (ring.radius + intensity * 30);
                    const y2 = centerY + Math.sin(angle) * (ring.radius + intensity * 30);
                    
                    const hue = (ringIndex * 45 + i * 5) % 360;
                    const color = `hsl(${hue}, 80%, ${50 + intensity * 30}%)`;
                    
                    this.ctx.strokeStyle = color;
                    this.ctx.lineWidth = 2 + intensity * 4;
                    this.ctx.beginPath();
                    this.ctx.moveTo(x1, y1);
                    this.ctx.lineTo(x2, y2);
                    this.ctx.stroke();
                    
                    // 发光效果
                    this.ctx.shadowColor = color;
                    this.ctx.shadowBlur = intensity * 15;
                    this.ctx.stroke();
                    this.ctx.shadowBlur = 0;
                }
            }
        });
    }

    drawCenterPulse(frequencyData, centerX, centerY) {
        const avgIntensity = frequencyData.reduce((sum, val) => sum + val, 0) / frequencyData.length / 255;
        
        if (avgIntensity > 0.2) {
            const pulseRadius = avgIntensity * 100;
            
            const gradient = this.ctx.createRadialGradient(
                centerX, centerY, 0,
                centerX, centerY, pulseRadius
            );
            
            gradient.addColorStop(0, `rgba(255, 255, 255, ${avgIntensity})`);
            gradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
            
            this.ctx.fillStyle = gradient;
            this.ctx.beginPath();
            this.ctx.arc(centerX, centerY, pulseRadius, 0, Math.PI * 2);
            this.ctx.fill();
        }
    }

    resize(width, height) {
        // 调整最大半径
        this.settings.maxRadius = Math.min(width, height) / 3;
        this.initializeRings();
    }
}

// 注册可视化器到全局注册表
if (typeof window !== 'undefined') {
    if (!window.visualizerRegistrations) {
        window.visualizerRegistrations = [];
    }
    window.visualizerRegistrations.push({
        id: 'circular-pulse',
        class: CircularPulseVisualizer
    });
}

window.CircularPulseVisualizer = CircularPulseVisualizer;
